<?php

namespace core\libs;

use core\libs\config;

class route {
    protected $route_conf = [];
    public function __construct() {
        // 获取路由配置
        $this->route_conf = config::get(APP_MODULAR, 'ROUTE');
        // 分析path
        $this->path_analysis();
        //加载控制器/方法
        $this->loadCtrl();
    }

    // 剥离控制器和方法，然后把剩余变量添加到GET内
    protected function path_analysis() {
        // 参数为空直接跳出
        if(!isset($_GET["s"])) return;

        // 处理路径
        $paths = array_filter(explode("/", $_GET["s"]));

        // 放入新数组
        foreach($paths as $key => $val) {
            $temp[] = $val;
        }

        //如果有模块，则过滤掉。
        if(isset($temp[0]) && $temp[0] == APP_MODULAR) {
            unset($temp[0]);
            // 放入新数组
            $temp_modular = $temp;
            $temp = [];
            foreach($temp_modular as $key => $val) {
                $temp[] = $val;
            }
        }


        // 判断控制器是否存在
        if(isset($temp[0]) && $temp[0] != "page") {
            // 如果是已经配置过的路由，则直接获取
            if(array_key_exists( $temp[0], $this->route_conf["paths"] )) {
                $this->route_conf["default_controller"] = $this->route_conf["paths"][$temp[0]][0];
                $this->route_conf["default_action"] = $this->route_conf["paths"][$temp[0]][1];
                unset($temp[0]);
            }
            else {
                $this->route_conf["default_controller"] = $temp[0];
                unset($temp[0]);

                // 判断方法是否存在
                if(isset($temp[1]) && $temp[1] != "page") {
                    $this->route_conf["default_action"] = $temp[1];
                    unset($temp[1]);
                }
            }
        }

        // 添加到get全局变量
        if(isset($temp)) $this->routeToGet( $temp );
    }

    // 添加参数到get全局变量
    protected function routeToGet($paths = []) {
        // 放入新数组
        foreach($paths as $key => $val) {
            $temp[] = $val;
        }

        if(isset($temp)) {
            $i = 0;
            $count = count($temp) + 2;
            while ($i < $count){
                if(isset($temp[$i+1])) {
                    $_GET[$temp[$i]] = $temp[$i+1];
                    $_REQUEST[$temp[$i]] = $temp[$i+1];
                }
                $i+=2;
            }
        }
    }

    // 加载控制器/方法
    protected function loadCtrl() {
        // 获取控制器与方法
        $action = $this->route_conf["default_action"];
        $controller = '\app\\'. APP_MODULAR .'\controller\\'. $this->route_conf["default_controller"];
        // 添加到全局变量
        $GLOBALS["action"] = $this->route_conf["default_action"];
        $GLOBALS["controller"] = $this->route_conf["default_controller"];

        if(class_exists($controller)) {
            if (method_exists($controller, $action)) {
                (new $controller)->$action();
            }
            else {
                throw new \Exception("方法不存在" . $action);
            }
        }
        else {
            throw new \Exception("找不到控制器" . $controller);
        }
    }
}